#!/usr/bin/python
# -*- coding:utf-8 -*-
from obs import ObsClient
import os
# pip install esdk-obs-python
# 配置信息（严格对齐moxing的成功路径）
AK = 'GK2KEZTVRRGWN2L9FQKT'
SK = 'lbKRy9E2PjmWsfv5kB39ck3lW3RV1pXpNmKlQ6Rv'
server = 'obs.cn-north-305.tjaicc.com'
bucketName = 'dtu'  # moxing路径中obs://dtu/ 的 "dtu"
obs_folder = 'dtu_training'  # moxing路径中 "dtu_training"（无末尾斜杠，与moxing一致）
local_folder = '/home/ma-user/work/dtu_training'  # 与moxing本地路径一致
# if __name__ == '__main__':
#     # download_file_v1()
#     import moxing as mox
#     #下载一个OBS文件夹sub_dir_0，从OBS下载至Notebook
#     mox.file.copy_parallel('obs://dtu/dtu_training', '/home/ma-user/work/dtu_training')

# 配置信息
AK = 'GK2KEZTVRRGWN2L9FQKT'
SK = 'lbKRy9E2PjmWsfv5kB39ck3lW3RV1pXpNmKlQ6Rv'
server = 'obs.cn-north-305.tjaicc.com'
bucketName = 'dtu'  # OBS桶名
obs_folder = 'dtu_training'  # OBS上的文件夹路径
local_folder = '/home/ma-user/work/dtu_training'  # 本地目标文件夹
# ./obsutil config -i=GK2KEZTVRRGWN2L9FQKT -k=lbKRy9E2PjmWsfv5kB39ck3lW3RV1pXpNmKlQ6Rv -e=obs.cn-north-305.tjaicc.com
# ./obsutil cp  obs://dtu/dtu_training/ /home/ma-user/work/dtu_training -r -f
def download_obs_folder():
    # 创建OBS客户端
    obs_client = ObsClient(
        access_key_id=AK,
        secret_access_key=SK,
        server=server
    )
    
    try:
        # 确保本地目录存在
        os.makedirs(local_folder, exist_ok=True)
        
        # 列出OBS文件夹中的所有对象
        resp = obs_client.listObjects(bucketName, prefix=obs_folder+'/')
        if resp.status < 300:
            print(f'开始下载OBS文件夹: obs://{bucketName}/{obs_folder}/ 到 {local_folder}')
            
            for content in resp.body.contents:
                # 获取对象键（相对路径）
                object_key = content.key
                
                # 跳过文件夹本身
                if object_key.endswith('/'):
                    continue
                
                # 构建本地文件路径
                relative_path = object_key[len(obs_folder)+1:]
                local_file_path = os.path.join(local_folder, relative_path)
                
                # 确保本地目录存在
                os.makedirs(os.path.dirname(local_file_path), exist_ok=True)
                
                # 下载文件
                resp_get = obs_client.getObject(bucketName, object_key, downloadPath=local_file_path)
                if resp_get.status < 300:
                    print(f'下载成功: {object_key} -> {local_file_path}')
                else:
                    print(f'下载失败: {object_key}, 错误码: {resp_get.status}')
                    
            print('文件夹下载完成')
        else:
            print(f'无法列出OBS文件夹内容, 错误码: {resp.status}')
            
    except Exception as e:
        print(f'发生异常: {str(e)}')
    finally:
        # 关闭OBS客户端
        if 'obs_client' in locals():
            obs_client.close()

if __name__ == '__main__':
    download_obs_folder()